package com.huawei.crm.web.controller;

import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Permission;
import com.huawei.crm.query.QueryObject;
import com.huawei.crm.service.IPermissionService;
import com.huawei.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService service;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"权限列表","permission:list"}, logical = Logical.OR)
    public String list(Model model, QueryObject qo) {
        PageInfo<Permission> pageInfo = service.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"权限删除","permission:delete"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            if (id != null) {
                service.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲,删除失败");
        }
        return result;
    }

    @RequestMapping("/reload")
    @RequiresPermissions(value = {"权限加载","permission:reload"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult reload(Long id) {
        service.reload();
        JsonResult result = new JsonResult();
        try {
            if (id != null) {
                service.reload();
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲,删除失败");
        }
        return result;
    }
}
